package com.google.code.mochaccino.framework.search;
/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.Sort;

/** Index Client which supports i18N */
public final class SearchIndexMultiLingual {
	private Map<Locale,SearchIndex> localizedClients = null;

	/** Client */
	public SearchIndexMultiLingual( File baseDir, String defaultField, Map<Locale,Analyzer> localization ) throws IOException {
		localizedClients = new HashMap<Locale,SearchIndex>();
		for ( Entry<Locale,Analyzer> e : localization.entrySet() ) {
			Locale loc = e.getKey();
			String localeName = loc.getLanguage();
			if ( loc.getVariant() != null ) {
				localeName = localeName + "_" + loc.getVariant();
			}
			File dir = new File( baseDir, localeName );
			Analyzer analyzer = e.getValue();
			localizedClients.put( loc, new SearchIndex( dir, defaultField, analyzer ) );
		}
	}

	/** Search */
	public Iterator<Document> search( Locale locale, String queryString, Filter filter, int maxResultCount ) throws ParseException, IOException {
		return localizedClients.get( locale ).search( queryString, filter, maxResultCount );
	}

	/** Search */
	public Iterator<Document> search( Locale locale, String queryString, Filter filter, int max, Sort sort ) throws ParseException, IOException {
		return localizedClients.get( locale ).search( queryString, filter, max, sort );
	}

	/** Search */
	public Iterator<Document> search( Locale locale, String queryString, int maxResultCount ) throws ParseException, IOException {
		return localizedClients.get( locale ).search( queryString, maxResultCount );
	}
}
